package com.sayimo.maker.commons.ali.pay;

import java.io.IOException;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.sayimo.framework.common.MessageObject;
import com.sayimo.maker.commons.Constant;
import com.sayimo.maker.commons.ali.sms.util.HttpUtils;
import com.sayimo.maker.commons.utils.JSONUtil;

public class AliScancodePay {
	
	public static Map creatOrCode(Map _paramMap) throws AlipayApiException, MalformedURLException, IOException {
		Map resultMap = new HashMap();
		//公共参数
		Map<String,Object> publicParams = new HashMap<>();
		publicParams.put("method", "alipay.trade.precreate");
		publicParams.put("app_id", AlipayUtil.ALIPAY_APPID);
		publicParams.put("charset", "utf-8");
		publicParams.put("sign_type", "RSA2");
		publicParams.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		
		//业务参数
		 Map<String, String> pcont  = new HashMap<String, String>();
	     String subject= (String) _paramMap.get("goodsName");
	     String out_trade_no= (String) _paramMap.get("ordersNo");
	     String total_amount= String.valueOf(_paramMap.get("payPrice"));
	     
	     pcont.put("out_trade_no", out_trade_no); 
	     pcont.put("total_amount", total_amount);
	     pcont.put("subject", subject);
	     
	     publicParams.put("biz_content", JSONUtil.writeValueAsString(pcont));
	     
	     String _param=AlipaySignature.getSignContent(pcont);
	     String rsaSign = AlipaySignature.rsaSign(_param, AlipayUtil.APP_PRIVATE_KEY, "utf-8","RSA2"); 
	     publicParams.put("sign", rsaSign);
	     
	     String param = JSONUtil.writeValueAsString(publicParams);
	     MessageObject result = com.sayimo.maker.commons.utils.HttpUtils.postMethod("https://openapi.alipay.com/gateway.do", param);
	     String url=JSONUtil.writeValueAsString(result);
	     System.out.println(result);
	     Map dataMap =new HashMap<>();
	     dataMap.put("orderStr", dataMap);
	     resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
		 resultMap.put(Constant.DATA, dataMap);	
		 return resultMap;	
	}
	
}
